package com.slkj.shilixiaoyuanapp.ui.user;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.UserEntity;
import com.slkj.shilixiaoyuanapp.entity.UserInfosEntity;
import com.slkj.shilixiaoyuanapp.net.callback.BaseFunctionCallBack;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.service.UserService;
import com.slkj.shilixiaoyuanapp.ui.main.MainActivity;
import com.slkj.shilixiaoyuanapp.util.TxtUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;

@ActivityInfo(layout = R.layout.activity_login, isShowActionBar = false)
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_wrong_phone)
    TextView wrongPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_wrong_pwd)
    TextView wrongPwd;
    @BindView(R.id.tv_forget_pwd)
    TextView forgetPwd;
    private String schoolName;

    private int schoolID;

    @Override
    protected void init() {
        initListener();
    }

    private void initListener() {
        forgetPwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == forgetPwd) {
            showToast("忘记密码啦！");
        }
    }

    @OnClick(R.id.tv_login)
    void login() {
        if (etPhone.getText().toString().isEmpty()) {
            showToast("请输入账号");
            return;
        }
//        if (!TxtUtil.isMobileNum(etPhone.getText().toString())) {
//            showToast("请输入正确手机号");
//            return;
//        }
        if (etPwd.getText().toString().isEmpty()) {
            showToast("请输入密码");
            return;
        }
        wrongPhone.setVisibility(View.GONE);
        wrongPwd.setVisibility(View.GONE);
        HttpHeper.get().create(UserService.class).userLogin(etPhone.getText().toString(), etPwd.getText().toString())
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<UserEntity>(true, this) {
                    @Override
                    public void onCallBackSuccess(UserEntity data) {
                        UserRequest.getInstance().setUser(data);
                        JPushInterface.setAlias(LoginActivity.this, 1, data.getUser().getULoginUserId() + "");
                        startActivity(MainActivity.class);
                        finish();
                    }

                    @Override
                    public void onFail(String mes) {
                        showToast(mes);
                        if (mes.equals("无此账号")) {
                            wrongPhone.setVisibility(View.VISIBLE);
                        } else if (mes.equals("密码错误")) {
                            wrongPwd.setVisibility(View.VISIBLE);
                        }
                    }
                });

//        HttpConfig.tsetBASE_URL(1);
//        Observable<CommonResult<UserInfosEntity>> loginOne = HttpHeper.get().create(UserService.class).userLogin(etPhone.getText().toString(), etPwd.getText().toString());
//        HttpConfig.tsetBASE_URL(2);
//        UserService userService = HttpHeper.get().create(UserService.class);
//        loginOne.flatMap(new BaseFunctionCallBack<UserInfosEntity, UserEntity>() {
//                    @Override
//                    public ObservableSource<CommonResult<UserEntity>> back(UserInfosEntity result) {
//                        schoolID = result.getSchool().get(0).getId();
//                        schoolName = result.getSchool().get(0).getName();
//                        return userService.userLogin(result.getUser().getId(), result.getSchool().get(0).getId());
//                    }
//                })
//                .compose(bindToLifecycle())
//                .compose(getThread())
//                .subscribe(new CommonCallBack<UserEntity>(true, this) {
//                    @Override
//                    public void onCallBackSuccess(UserEntity data) {
//                        data.getUser().setSchoolID(schoolID);
//                        data.getUser().setSchoolName(schoolName);
//                        UserRequest.getInstance().setUser(data);
//                        JPushInterface.setAlias(LoginActivity.this, 1, data.getUser().getULoginUserId() + "");
//                        startActivity(MainActivity.class);
//                        finish();
//                    }
//
//                    @Override
//                    public void onFail(String mes) {
//                        showToast(mes);
//                        if (mes.equals("无此账号")) {
//                            wrongPhone.setVisibility(View.VISIBLE);
//                        } else if (mes.equals("密码错误")) {
//                            wrongPwd.setVisibility(View.VISIBLE);
//                        }
//                    }
//
//                });
    }

}
